package dk.nordahl.btremote;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;


import dk.nordahl.btremote.R;

public class Command  extends Activity{
	
	private Button btnShutdDown;
	private Spinner cmdSpinner;

	private BluetoothDevice myDevice;
	private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

	private BluetoothSocket btSocket;

	private OutputStream OPS;
	private BTRemote btr;

	private ArrayList<String>cmdExecute;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		
	setContentView(R.layout.command_remote);
	btnShutdDown = (Button)findViewById(R.id.btnExecute);
	cmdSpinner = (Spinner)findViewById(R.id.commandList);
	btr = (BTRemote)getApplication();
	myDevice = btr.getBtDevice();
    	try {
			btSocket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
			btr.setBtSocket(btSocket);
		} catch (IOException e) {
			Log.e("Socket error", "temp sockets not created", e);
			e.printStackTrace();
		}
    	btr.Connect();
    	OPS = btr.getOPS();
    	addCommands();
    	btnShutdDown.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				int position = cmdSpinner.getSelectedItemPosition();
				String cmd = cmdExecute.get(position);
				writeCommand(cmd);	
			}
		});
	}
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
	       return true;
	    }
	    return super.onKeyUp(keyCode, event);
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	     super.onKeyDown(keyCode, event);
	if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
	{
	    String command = "arrowLeft";
	    writeCommand(command);
	    return true;
	}
	if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
	{
	    String command = "arrowRight";
	    writeCommand(command);
	    return true;
	}
	return true;
	}
	private void addCommands()
	{
		//List to be displayed in the spinner
		ArrayList<String> commandListView = new ArrayList<String>();
		commandListView.add("Shutdown computer");
		commandListView.add("Restart computer");
		commandListView.add("Lock computer");
		
		//List to contain the actual commands to be sent through Bluetooth
		cmdExecute = new ArrayList<String>();
		cmdExecute.add("shutdown");
		cmdExecute.add("restart");
		cmdExecute.add("lock");
		
		
		ArrayAdapter<String> cmdAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, commandListView);
		cmdAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cmdSpinner.setAdapter(cmdAdapter);
	
	}
	private void writeCommand(String command)
	{
		String out = command + "\n";
		byte[] cmd = out.getBytes();
		try {

		    OPS.write(cmd);

		}
		catch (IOException e){
			
			Log.e("Command error", out + " could not be executed");
		}
		catch(NullPointerException e)
		{
			e.printStackTrace();
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.btremote_main, menu);
		
		menu.getItem(2).setVisible(false);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item) {	
		
		switch(item.getItemId())
		{
			case R.id.device_menu:
			finish();
				return true;
			case R.id.touchpad_menu:
				writeCommand("touchpad");
				finish();
				Intent startTouchpad = new Intent(this, Touchpad.class);
				startActivity(startTouchpad);
				return true;

			default:
				return super.onOptionsItemSelected(item);
		}
	}
}
